﻿namespace mVids
{
    using MediaPortal.ServiceImplementations;
    using System;
    using System.IO;

    public class folderParser
    {
        public mVids.musicVideo parseFolder(mVids.musicVideo vid, string mask, string path)
        {
            if (mask != "IGNORE")
            {
                string[] strArray = mask.Split(@"\".ToCharArray());
                string[] strArray2 = vid.Path.Replace(path, "").Split(@"\".ToCharArray());
                if (strArray2.Length == strArray.Length)
                {
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i] == "%artist%")
                        {
                            vid.artist = strArray2[i];
                            vid.maskedName = true;
                        }
                        if (strArray[i] == "%tags%")
                        {
                            vid.aI.tags = strArray2[i];
                        }
                        if (strArray[i] == "%last.fm%")
                        {
                            vid.aI.lfmName = strArray2[i];
                            vid.aI.customlfmName = true;
                        }
                    }
                    Log.Info("Music Video DB - Parsed artist={0}, tags=(1) from folder", new object[] { vid.artist, vid.aI.tags });
                }
                if (File.Exists(vid.Path.Remove(vid.Path.LastIndexOf(@"\") + 1) + "info.txt"))
                {
                    string[] strArray3 = File.ReadAllLines(vid.Path.Remove(vid.Path.LastIndexOf(@"\") + 1) + "info.txt");
                    foreach (string str in strArray3)
                    {
                        if (str.StartsWith("artist="))
                        {
                            vid.artist = str.Replace("artist=", "");
                            vid.maskedName = true;
                        }
                        if (str.StartsWith("last.fm="))
                        {
                            vid.aI.lfmName = str.Replace("last.fm=", "");
                            vid.aI.customlfmName = true;
                        }
                        if (str.StartsWith("tags=") && (vid.aI.tags != ""))
                        {
                            vid.aI.tags = vid.aI.tags + "|" + str.Replace("tags=", "");
                        }
                        Log.Info("Music Video DB - Parsed artist={0}, tags=(1), last.fm={2} from info.txt", new object[] { vid.artist, vid.aI.tags, vid.aI.lfmName });
                    }
                }
            }
            return vid;
        }
    }
}

